 package cn.itcast.dom4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

public class Demo1 {

	//读取xml文档数据：<书名>Java就业培训教程</书名>
	@Test
	public void read() throws Exception{
		
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element root = document.getRootElement();
		Element bookname = root.element("书").element("书名");
		System.out.println(bookname.getText());
	}
	
	//<书 name="yyyyyyy">
	@Test
	public void readAttr() throws Exception{
		
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element root = document.getRootElement();
		String value = root.element("书").attributeValue("name");
		System.out.println(value);
	}
	
	//向xml文档中添加<售价>19元</售价>
	@Test
	public void add() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element price = DocumentHelper.createElement("售价");
		price.setText("19元");
		
		document.getRootElement().element("书").add(price);
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
		writer.write(document);  //utf-8
		writer.close();
	}
	
	//修改：<售价>109</售价>  为209
	@Test
	public void update() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element price = (Element) document.getRootElement().element("书").elements("售价").get(1);
		price.setText("209元");
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
		writer.write(document);  //utf-8
		writer.close();
		
	}
	//删除：<售价>109</售价>
	@Test
	public void delete() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element price = (Element) document.getRootElement().element("书").elements("售价").get(0);
		price.getParent().remove(price);
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
		writer.write(document);  //utf-8
		writer.close();
	}

	
	//向指定位置增加售价结点
	@Test
	public void add2() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element price = DocumentHelper.createElement("售价");
		price.setText("19元");
		
		List list = document.getRootElement().element("书").elements();
		list.add(1, price);
		
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
		writer.write(document);  //utf-8
		writer.close();
		
	}
	
	@Test
	public void findWithXpath() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		
		Element e = (Element) document.selectNodes("//书名").get(1);
		System.out.println(e.getText());
	}
	
	@Test
	public void findUser() throws Exception{
		String username = "aaa";
		String password = "1233";
		
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/users.xml"));
		
		Element e = (Element) document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
		if(e!=null){
			System.out.println("让用户登陆成功！！");
		}else{
			System.out.println("用户名和密码不正确！！");
		}
	
	}
}
